<?php


class Api_buy_finsh extends Base_Controller
{
	
	
	public function __construct()
	{
		parent::__construct();
	}
	
	public function outputdata($data)
	{	
		
		$this->db->limit($data['QUESTNUM']);
 
		if($data['QBEFORE'] == 'Y' && !empty($data['ENDDATE2'])){
			$this->db->where('endDate2 <',$data['ENDDATE2']);			
		}elseif($data['QBEFORE'] == 'N' && !empty($data['ENDDATE2'])){	
			$this->db->where('endDate2 >',$data['ENDDATE2']);
		}else{
			//$this->db->where('endDate2 <',$data['ENDDATE2']);
		}
		$result = $this->db->where('enabled', 2)->order_by('endDate2','DESC')->get('cj_reward')->result_array();
	
		$res = array();
		foreach($result as $k=> $v){
			$tmp['drawNo'] = (int)$v['id'];
			$tmp['title'] = $v['infos'];
			$tmp['prices'] = (float)$v['singleprice'];

			$tmp['beginDate'] = date('Y-m-d',$v['beginDate']);
			$tmp['endDate'] = NULL;
			$tmp['endDate'] = date('Y-m-d',$v['endDate']);
			$tmp['endDate2'] = $v['endDate2'];
			$tmp['singleprice'] = $v['singleprice'];
			$tmp['imageUrl'] = site_url('/upload/reward/').'/'.$v['rewardpic'];
			$tmp['desc'] = $v['content'];
			$tmp['bHaveDrawed'] = $v['enabled'];
			$tmp['drawType'] = (int)$v['showtype'];
			
			$tmp['themename'] = '1';
			$tmp['minNumber'] = 1;
			$tmp['maxNumber'] = (int)$v['maxnumber'];
			$tmp['maxAllowSelectNumber'] = 3;
			$tmp['minAllowSelectNumber'] = 1;
			$tmp['attendCount'] = (int)$this->_getJoinPersion($v['id']);

			//$tmp['products'] = $this->getproducts($v['id']);
			//$tmp['winning'] = $this->get_finsh_reward($v['id']);
			$tmp['products'] = $this->get_finsh_reward($v['id']);
			//print_r($tmp['products']);
			$res[] = $tmp;
		}
		
		header('Content-Type:text/html; charset=utf-8', false);
		$outputdata['TAG'] = $data['TAG'];
		$outputdata['DATA'] = $res;
		//print_r($outputdata);
	
		echo json_encode($outputdata);
		
	}
	
	/**
	 * 
	 * 活动奖品...
	 * @param int $id
	 */
	private function getproducts($id)
	{
		$flag = $this->db->select('product.productname,product.id,product.ptype,product.content,lottery.rnum,lottery.rewardid')
				->where('lottery.rewardid',$id)
				->join('cj_products as product','lottery.productid = product.id')
				->from('cj_lottery as lottery')
				->get()->result_array();
		$result = array();
		if(!empty($flag))
		{
			foreach($flag as $v)
			{
				$tmp['id'] = (int)$v['id'];
				$tmp['productName'] = $v['productname'];
				$tmp['ptype'] = (int)$v['ptype'];
				$tmp['content'] = $v['content'];
				$result[] = $tmp;
			}
		}
		return $result;
	}
	
	
	private function _getJoinPersion($id)
	{
		$persion = 0;
		$lottery = array();
		$lottery = $this->db->select('rewardid,uid')
				->from('cj_lottery')
				->where('rewardid',$id)
				->get()
				->result_array();
		if(!empty($lottery)){
			foreach($lottery as $key => $value){
				if(!empty($value['uid'])){
					$p[] = $value['uid'];
				}
			}
			if(!empty($p)){
				$parray= array_unique($p);
				$persion = count($parray);
			}
		}
		
		return $persion;
	}
	
	/**
	 *结束活动中奖情况
	 */
	
	private function get_finsh_reward($id)
	{
		$flag = $this->db->select('product.productname,product.id,product.ptype,product.content,lottery.rnum,lottery.rewardid,user.uid,user.txusername,user.headpic')
				->where('lottery.rewardid',$id)
				->where('lottery.uid >',0)
				->where('lottery.productid >',0)
				->join('cj_products as product','lottery.productid = product.id')
				->join('cj_users as user','user.uid = lottery.uid')
				->from('cj_lottery as lottery')
				->get()->result_array();
		
		$result = array();
		if(!empty($flag))
		{
			foreach($flag as $v)
			{
				$tmp['id'] = (int)$v['id'];
				$tmp['productName'] = $v['productname'];
				$tmp['user'] = $v['txusername'];
				$tmp['uid'] = (int)$v['uid'];
				$tmp['ptype'] = (int)$v['ptype'];
				$tmp['buynum'] = (int)$v['rnum']; 
				$tmp['facephoto'] = empty($v['headpic']) ? null : site_url('/upload/users/').'/'.$v['headpic'];
				$tmp['content'] = $v['content'];
				$result[] = $tmp;
			}
		}

		return $result;
	}
}